package com.swak.fx.support;

import java.util.Optional;

import com.swak.exception.InvokeException;
import com.swak.fx.support.ui.UIDialog;

import javafx.scene.control.ButtonType;
import javafx.stage.Stage;

/**
 * 提供基本的弹出窗体的操作；
 * 
 * @author 618lf
 */
public class Dialog extends AbstractPage {

	protected UIDialog inner;

	@Override
	public void initializeParts() {
		super.initializeParts();
	}

	public final Optional<ButtonType> show(Stage stage) {
		if (inner == null) {
			this.initDialog(stage);
		}
		return inner.showAndWait();
	}

	private void initDialog(Stage stage) {
		this.inner = new UIDialog(stage);
		this.inner.contentNodeProperty().set(root);
	}

	public final void hide() {
		inner.hide();
	}

	@Override
	public void destory() {
		super.destory();
		if (inner != null) {
			inner.destory();
		}
	}

	public final static <T extends Popup> T New(Class<T> theClass) {
		try {
			T t = theClass.getDeclaredConstructor().newInstance();
			t.init();
			return t;
		} catch (Exception e) {
			throw new InvokeException(e);
		}
	}
}